<template>
	<u-popup :show="show" mode="center" @close="handleShowCollctPopup(false)" closeable>
		<view class="his-pop padd-top30 padd-left32 padd-right32">
			<view class="font_s28 cr-333 mar-bottom31">创建收藏夹</view>
			<view class="content padd-bottom30">
				<view class="flex mar-bottom19 ">
					<u-input type="text" v-model="favoritesName" placeholder="请输入收藏夹名称"></u-input>
				</view>
				<u-button type="primary" text="确定" @click="handleConfirmFavorites"></u-button>
			</view>
		</view>
	</u-popup>
</template>

<script>
	export default {
		props: ['show', 'favorites'],
		name: "t-collectpop",
		data() {
			return {
				favoritesName: ''
			};
		},
		watch: {
			favorites: {
				handler() {
					this.favoritesName = this.favorites ? this.favorites.favoritesName : ''
				},
				deep: true,
				immediate: true
			},
		},

		methods: {
			handleShowCollctPopup(flag) {
				if (flag) {
					this.favoritesName = this.favorites ? this.favorites.favoritesName : ''
				} else {
					this.$emit('close')
					this.favoritesName = ''
				}
			},
			//添加收藏夹
			handleAddUserFavorites() {

				return this.$http.post(
					`/yaolexue-service-portal/userFavorites/addUserFavorites`, {
						favoritesName: this.favoritesName,
					}, {
						isNeedLoading: true
					}).then(res => {
					if (res.code === 200) {
						this.handleShowCollctPopup(false)
					}
				})
			},
			//收藏夹编辑确定
			handleConfirmFavorites() {
				if (this.favorites) {
					this.handleUpdateUserFavorites()
				} else {
					this.handleAddUserFavorites()
				}
			},
			//修改收藏夹
			handleUpdateUserFavorites() {
				return this.$http.put(
					`/yaolexue-service-portal/userFavorites/updateUserFavorites`, {
						favoritesName: this.favoritesName,
						id: this.favorites.id
					}, {
						isNeedLoading: true
					}).then(res => {
					if (res.code === 200) {
						this.handleShowCollctPopup(false)
					}
				})
			},
		},
	}
</script>

<style lang="scss">

</style>